package com.zwt.service.impl;

import com.zwt.constant.RedisConstant;
import com.zwt.constant.RedisKeyUtil;
import com.zwt.dao.TAccountUserMapper;
import com.zwt.model.TAccountUser;
import com.zwt.model.TAccountUserExample;
import com.zwt.redis.Redis;
import com.zwt.service.TAccountUserService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Created by zwt on 2018/9/5.
 */
@Service
public class TAccountUserServiceImpl implements TAccountUserService{

    @Autowired
    private TAccountUserMapper tAccountUserMapper;

    @Autowired
    private Redis redis;


    @Override
    public TAccountUser queryById(Long id) {
        TAccountUser tAccountUser = redis.get(RedisKeyUtil.getUserCache(id), TAccountUser.class);
        if(null==tAccountUser){
            tAccountUser = tAccountUserMapper.selectByPrimaryKey(id);
            redis.set(RedisKeyUtil.getUserCache(id),tAccountUser, RedisConstant.MONTH_1*6);
        }
        return tAccountUser;
    }

    @Override
    public TAccountUser selectByPhone(String phone) {
        TAccountUserExample tAccountUserExample = new TAccountUserExample();
        TAccountUserExample.Criteria criteria = tAccountUserExample.createCriteria();
        criteria.andUserPhoneEqualTo(phone);
        List<TAccountUser> tAccountUsers = tAccountUserMapper.selectByExample(tAccountUserExample);
        if(CollectionUtils.isNotEmpty(tAccountUsers) && tAccountUsers.size()==1){
            return tAccountUsers.get(0);
        }
        return null;
    }

    @Override
    public int insert(TAccountUser tAccountUser) {
        return tAccountUserMapper.insertSelective(tAccountUser);
    }
}
